package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.common.ResultContentTypeVo;
import com.zp.domain.ModuleType;
import com.zp.util.Constant;

/**
 * test module type services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/12
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunModuleTypeServices {

	private static final Log logger = LogFactory
			.getLog(RunModuleTypeServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest/";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	@Test
	public void testAddType() {
		try {
			ModuleType type = new ModuleType();
			type.setName("创业指导");
			type.setCode("mo05");
			type.setParentCode("mo");
			ClientResponse response = service.path("mtype/addType")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, type);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/** @Test **/
	public void testfindTypeById() {
		String id = "402881e844b6bda90144b6c07fe60004";
		ClientResponse response = service.path("mtype/getType")
				.queryParam("typeId", id).post(ClientResponse.class);
		ResultContentTypeVo result = response
				.getEntity(ResultContentTypeVo.class);
		Assert.assertNotNull(result.getType());
	}

	/**@Test**/
	public void testUpdateType() {
		ModuleType t = new ModuleType();
		t.setId("402881e844b6bda90144b6bf20280001");
		t.setName("模块管理");
		t.setCode("mo");
		ClientResponse response = service.path("mtype/updateType")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.put(ClientResponse.class, t);
		Assert.assertEquals(Constant.OK,
				response.getEntity(ResultCommonVo.class).getMessage());

	}

	/** @Test **/
	public void testDeleteType() {
		ClientResponse response = service.path("mtype/deleteType")
				.queryParam("typeId", "402881e844b6a7d30144b6ab12370001")
				.delete(ClientResponse.class);
		Assert.assertEquals(Constant.OK,
				response.getEntity(ResultCommonVo.class).getMessage());

	}

	/** @Test **/
	public void testGetTypeByCritera() {
		ClientResponse response = service.path("mtype/getTypes")
				.queryParam("pageNo", "1").queryParam("pageSize", "10")
				.queryParam("codeName", "mo01")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
		ResultContentTypeVo result = response
				.getEntity(ResultContentTypeVo.class);
		Assert.assertEquals(1, result.getTotalCount());
	}

}
